<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="类目名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['categoryName',validatorRules.categoryName]" placeholder="请输入类目名称"></a-input>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['sort',validatorRules.sort]" placeholder="请输入排序" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select v-decorator="['level',validatorRules.level]" @change="updateLevel"
                    placeholder="请选择等级" :allowClear="true">
            <a-select-option :value="1">一级</a-select-option>
            <a-select-option :value="2">二级</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-decorator="['status',validatorRules.status]">
            <a-radio :value="0">
              上架
            </a-radio>
            <a-radio :value="1">
              下架
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="上级类目" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 2">
          <a-select v-decorator="['parentId',validatorRules.parentId]"
                    placeholder="请选择上级类目" :allowClear="true">
            <a-select-option v-for="item in categoryList"
                             :value="item.id"
                             :key="item.id">{{ item.categoryName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="就诊医院" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isVisitingHospital']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="就诊时间" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isTreatmentTime']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="就诊人" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isPatient']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isContactNumber']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="接送地址" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isPickUpAddress']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="收件信息" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isReceivingInformation']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="陪诊员" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isEscort']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item label="服务需求" :labelCol="labelCol" :wrapperCol="wrapperCol" v-show="model.level == 1">
          <a-radio-group v-decorator="['isServiceRequirements']">
            <a-radio :value="0">
              关闭
            </a-radio>
            <a-radio :value="1">
              开启
            </a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "MedicalEscortServiceCategoryModal",
    components: {
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          status: {
            rules: [{required: true, message: '请选择状态!'},]
          },
          categoryName: {
            rules: [{required: true, message: '请输入类目名称!'},]
          },
          sort: {
            rules: [{required: true, message: '请输入排序!'},]
          },
          level: {
            rules: [{required: true, message: '请选择等级!'},]
          },
        },
        url: {
          add: "/escort/medicalEscortServiceCategory/add",
          edit: "/escort/medicalEscortServiceCategory/edit",
          getCategoryList: "/escort/medicalEscortServiceCategory/getCategorySelectList",
        },
        categoryList: [],
      }
    },
    created() {
      this.getCategoryList();
    },
    methods: {
      updateLevel(value) {
        this.$set(this.model, "level", value);
        if (value == 1) {
          this.validatorRules = {
            categoryName: {
              rules: [{required: true, message: '请输入类目名称!'},]
            },
            sort: {
              rules: [{required: true, message: '请输入排序!'},]
            },
            level: {
              rules: [{required: true, message: '请选择等级!'},]
            },
          }
        } else {
          this.$set(this.validatorRules, "parentId", {
            rules: [{required: true, message: '请选择上级等级!'},]
          });
        }
      },
      add() {
        this.edit({
          isVisitingHospital: 0,
          isTreatmentTime: 0,
          isPatient: 0,
          isContactNumber: 0,
          isPickUpAddress: 0,
          isReceivingInformation: 0,
          isEscort: 0,
          isServiceRequirements: 0,
        });
      },
      edit(record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'categoryName', 'status', 'sort', 'parentId', 'level', 'isVisitingHospital', 'isTreatmentTime', 'isPatient', 'isContactNumber', 'isPickUpAddress', 'isReceivingInformation', 'isEscort', 'isServiceRequirements', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'categoryName', 'status', 'sort', 'parentId', 'level', 'isVisitingHospital', 'isTreatmentTime', 'isPatient', 'isContactNumber', 'isPickUpAddress', 'isReceivingInformation', 'isEscort', 'isServiceRequirements', 'createBy', 'createTime', 'updateBy', 'updateTime', 'delFlag'))
      },
      getCategoryList() {
        getAction(this.url.getCategoryList, {level: 1}).then((res) => {
          if (res.success) {
            this.categoryList = res.result;
          }
        });
      },
    }
  }
</script>